import time
from unittest import TestCase
from ddt  import  ddt,data,unpack
from data.ReadData import  ReadData
from Interface.Upload_Token_1 import Upload_Token_1
import os
import json
from Utils.UrlUtils import UrlUtils
from Utils.DBUtils import DBUtils
from configparser import ConfigParser

cfg = ConfigParser()
cfg.read(os.getcwd() + os.sep + "config.ini")
datafile = cfg.get("global","data_file")
# 读取global全局配置
iftest = cfg.get("global","test_all")

# 读取开发和测试环境请求地址
dev_url = cfg.get("global","dev_host")
test_url = cfg.get("global","test_host")


db =  DBUtils()
# 测试数据存放路径
data_path = os.path.abspath(os.getcwd() + os.sep + "data" + os.sep + datafile)

total_url = ""

if iftest == "false":
    total_url = test_url
else:
    total_url = dev_url

print("-------全局配置url前缀为：",total_url,"------------")

@ddt
class TestCount(TestCase):

    # 正常采集接口测试
    @data(*ReadData().readData(1,data_path))
    @unpack
    def testSuccessCount(self,id,data):
        jdata = json.loads(data)
        obj = Upload_Token_1()
        urlutils = UrlUtils()


        # 把这个接口换成调用gather接口即可
        rs1 = obj.gather(jdata,total_url)


        dict_data1 = rs1['data']  # 获取data数据体
        # 提取必要的全局数据，后面的所有接口中都要用的到
        token = urlutils.getUrlSingleParam("token", dict_data1)  # 提取data数据体中token数据
        req_id1 = rs1['req_id']  # 获取req_id编号
        clientId = urlutils.getUrlSingleParam("clientId", dict_data1)
        taxName = urlutils.getUrlSingleParam("taxName", dict_data1)
        taxCode = urlutils.getUrlSingleParam("taxCode", dict_data1)
        operNo = urlutils.getUrlSingleParam("operNo", dict_data1)
        operName = urlutils.getUrlSingleParam("operName", dict_data1)
        cooperatorIds = urlutils.getUrlSingleParam("cooperatorIds", dict_data1)
        finTaskId = urlutils.getUrlSingleParam("finTaskId", dict_data1)
        # product_id = urlutils.getUrlSingleParam("product_id", dict_data1)
        is_match = urlutils.getUrlSingleParam("is_match", dict_data1)
        order_id = urlutils.getUrlSingleParam("order_id", dict_data1)


        # 将上述参数放入rs1中
        rs1["token"] = token
        rs1["clientId"] = clientId
        rs1["taxName"] = taxName
        rs1["taxCode"] = taxCode
        rs1["operNo"] = operNo
        rs1["operName"] = operName
        rs1["cooperatorIds"] = cooperatorIds
        # rs1["product_id"] = product_id
        rs1["finTaskId"] = finTaskId
        rs1["is_match"] = is_match
        rs1["order_id"] = order_id



        # 访问第二个请求获取taskId参数
        rs2 = obj.getTaskId(rs1)
        rs1["taskId"] = rs2.get("taskId")

        # 访问同意并授权接口
        obj.AgreeAndAuth(rs1)

        # 停止60秒，60秒后采集数据库结果
        time.sleep(5)

        # 访问数据库是否存在当前采集数据
        '''
            ### 查看bangongyi_mongo表中数据有没有更新
        '''

        #USE  bangongyi_mongo;
        sql = ''' 
            
            SELECT 
                id,we_account_id,raw_message,`desc`,
                FROM_UNIXTIME(TIME,'%Y-%m-%d %H:%i:%s') ctime 
                FROM dv_debug_log WHERE we_account_id = 20211125 AND `desc` = 'actionJdrequestplug'
            ORDER BY ctime DESC limit 2;
        '''


        #USE  bangongyi;
        sql1 = '''
               select * from(
                SELECT 
                id,agentwe_id,task_id,company_name,tax_code,NAME,idcard,
                FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') ctime ,
                FROM_UNIXTIME(update_time,'%Y-%m-%d %H:%i:%s') uptime ,
                client_name
                FROM dv_loans_jd_request) tb
            where 
            `name`  = '{name}'
            and company_name = '{company_name}'
            and uptime>=DATE_SUB(NOW(),INTERVAL 2 MINUTE)
            ORDER BY uptime DESC;
        '''.format(name=jdata["name"],company_name=jdata["company_name"])
        print(sql1)
        res1 = db.select(sql1,db.gy_con,None)

        if len(res1) != 0:
            self.assertTrue(True,"采集结果成功")






















